package groupb6.shopsystem.service.impl;

import groupb6.shopsystem.mapper.*;
import groupb6.shopsystem.pojo.entity.Order;
import groupb6.shopsystem.pojo.entity.Store;
import groupb6.shopsystem.pojo.res.OrderProductRes;
import groupb6.shopsystem.pojo.entity.Product;
import groupb6.shopsystem.pojo.entity.User;
import groupb6.shopsystem.pojo.res.Products;
import groupb6.shopsystem.service.CustomerOrderService;
import groupb6.shopsystem.util.JWTUtil;
import groupb6.shopsystem.util.enums.IsReturnEnum;
import groupb6.shopsystem.util.enums.OrderStatusEnum;
import groupb6.shopsystem.util.enums.ResponseEnum;
import groupb6.shopsystem.util.response.ResponseException;
import groupb6.shopsystem.util.response.UniversalResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class CustomerOrderServiceImpl implements CustomerOrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private OrderProductMapper orderProductMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private ReturnMapper returnMapper;
    @Override
    public List<OrderProductRes> getOrder_ProductListByBuyerId() {
        User user = JWTUtil.getCurrentUser();
        Integer buyer_id = user.getId();
        List<Order> shoppingBagList = orderMapper.getOrderByBuyerId(buyer_id);
        List<OrderProductRes> orderProductList = new ArrayList<>();
        for (Order order : shoppingBagList) {
            OrderProductRes order_product = new OrderProductRes();
            Store store = storeMapper.getStoreBySellerId(order.getSellerId());

            order_product.setId(order.getId());
            order_product.setStoreName(store.getStoreName());
            order_product.setStatus(order.getStatus());
            order_product.setStatusReturn(order.getStatusReturn());
            order_product.setPriceAll(order.getPriceAll());
            order_product.setSellerId(order.getSellerId());
            order_product.setBuyerId(order.getBuyerId());
            order_product.setDestinationIdSeller(order.getDestinationIdSeller());
            order_product.setDestinationIdBuyer(order.getDestinationIdBuyer());
            order_product.setOrderTime(order.getOrderTime());
            order_product.setPayTime(order.getPayTime());
            order_product.setDeliverTime(order.getDeliverTime());
            order_product.setFinishTime(order.getFinishTime());
            order_product.setComment(order.getComment());
            order_product.setReturnForm(returnMapper.getReturnByOrderId(order.getId()));
            List<Products> productlist = new ArrayList<>();
            List<Integer> productidlist = orderProductMapper.getProductIdByOrderId(order.getId());
            for (Integer product_id : productidlist) {
                Product product = productMapper.getProductById(product_id);
                Integer number = orderProductMapper.getNumberOfProduct(order.getId(), product_id);
                Products product_one = new Products();
                product_one.setName(product.getName());
                product_one.setPrice(product.getPrice());
                product_one.setNumber(number);
                productlist.add(product_one);
            }
            order_product.setProductList(productlist);
            orderProductList.add(order_product);
        }
        Collections.reverse(orderProductList);
        return orderProductList;
    }

    @Override
    public OrderProductRes getOrder_ProductListByBuyerIdOne(Integer orderId) {
        User user = JWTUtil.getCurrentUser();
        Integer buyer_id = user.getId();
        Order order = orderMapper.selectById(orderId);
        if (!Objects.equals(buyer_id, order.getBuyerId())) {
            throw new ResponseException(ResponseEnum.USER_MATCH_ERROR.getCode(), ResponseEnum.USER_MATCH_ERROR.getMsg());
        }

        OrderProductRes order_product = new OrderProductRes();
        Store store = storeMapper.getStoreBySellerId(order.getSellerId());

        order_product.setId(order.getId());
        order_product.setStoreName(store.getStoreName());
        order_product.setStatus(order.getStatus());
        order_product.setStatusReturn(order.getStatusReturn());
        order_product.setPriceAll(order.getPriceAll());
        order_product.setSellerId(order.getSellerId());
        order_product.setBuyerId(order.getBuyerId());
        order_product.setDestinationIdSeller(order.getDestinationIdSeller());
        order_product.setDestinationIdBuyer(order.getDestinationIdBuyer());
        order_product.setOrderTime(order.getOrderTime());
        order_product.setPayTime(order.getPayTime());
        order_product.setDeliverTime(order.getDeliverTime());
        order_product.setFinishTime(order.getFinishTime());
        order_product.setComment(order.getComment());
        order_product.setReturnForm(returnMapper.getReturnByOrderId(order.getId()));
        List<Products> productlist = new ArrayList<>();
        List<Integer> productidlist = orderProductMapper.getProductIdByOrderId(order.getId());
        for (Integer product_id : productidlist) {
            Product product = productMapper.getProductById(product_id);
            Integer number = orderProductMapper.getNumberOfProduct(order.getId(), product_id);
            Products product_one = new Products();
            product_one.setName(product.getName());
            product_one.setPrice(product.getPrice());
            product_one.setNumber(number);
            productlist.add(product_one);
        }
        order_product.setProductList(productlist);

        return order_product;
    }

    @Override
    public UniversalResponse<?> userReceive(User currentUser, Integer orderId) {
        Order currentOrder = orderMapper.selectById(orderId);
        if (currentOrder.getBuyerId().equals(currentUser.getId())) {
            //订单属于买家
            if (currentOrder.getStatus().equals(OrderStatusEnum.SHIPPED.getStatus())) {
                //订单状态正确
                currentOrder.setStatus(OrderStatusEnum.RECEIVED.getStatus());
                orderMapper.updateById(currentOrder);
                ResponseEnum res = ResponseEnum.SUCCESS;
                return new UniversalResponse<>(res.getCode(),res.getMsg());
            } else {
                //订单状态不正确
                ResponseEnum res = ResponseEnum.ORDER_STATE_ERROR;
                return new UniversalResponse<>(res.getCode(), res.getMsg());
            }
        } else {
            //订单不是买家的
            ResponseEnum res = ResponseEnum.USER_NO_PERMISSION;
            return new UniversalResponse<>(res.getCode(), res.getMsg());
        }
    }

    @Override
    public UniversalResponse<?> returnOrderCustomer(Integer orderId, String reason) {
        Order order = orderMapper.getOrderByorderId(orderId);
        order.setStatusReturn(IsReturnEnum.IN_RETURN.getIsReturn());
        orderMapper.updateById(order);
        returnMapper.insertReturn(order.getId(), order.getPriceAll(), order.getDeliverTime(), order.getFinishTime(), -1, reason);
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    @Override
    public UniversalResponse<?> returnOrderSeller(Integer returnId, String trackingNumber) {
        returnMapper.returnProduct(returnId);
        returnMapper.updateTrackingNumber(returnId, trackingNumber);
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }
}
